package cn.semes.user.service.impl;

import cn.semes.basic.config.BussinessException;
import cn.semes.basic.constant.RegisterCon;
import cn.semes.user.domain.Logininfo;
import cn.semes.user.domain.User;
import cn.semes.user.dto.EmailRegistrationDto;
import cn.semes.user.dto.PhoneRegistrationDto;
import cn.semes.user.mapper.LogininfoMapper;
import cn.semes.user.mapper.UserMapper;
import cn.semes.user.service.IUserService;
import cn.semes.basic.service.impl.BaseServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author semes
 * @since 2022-11-24
 */
@Service
@Transactional
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
@Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;
    /**
     * 手机注册业务
     * @param dto
     */
    @Override
    public void register(PhoneRegistrationDto dto) {
        // 判断手机号是否注册
        User byPhone = userMapper.findByPhone(dto.getPhone());
        if(byPhone!=null){
            throw new BussinessException("手机已经注册");
        }
       String s = (String)redisTemplate.opsForValue().get(String.format(RegisterCon.REGISTER_KEY,dto.getPhone()));
        // 判断验证码是否正确
        if(!dto.getSmsCode().equals(s)){
            throw new BussinessException("验证码错误，请重试");
        }

        // 判断两次密码是否一致
        if(!dto.getPassword().equals(dto.getPasswordRepeat())){
            throw new BussinessException("两次密码不一致，请重试");
        }

        // 保存数据到数据库
        User user = dto.getUser();
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setDisable(false);
        logininfo.setType(1);
        logininfoMapper.add(logininfo);

        // 传入user中logininfoId
        user.setLogininfoId(logininfo.getId());
        userMapper.add(user);
    }

    @Override
    public void emailRegister(EmailRegistrationDto dto) {
        // 判断手机号是否注册
        User byPhone = userMapper.findByPhone(dto.getEmail());
        if(byPhone!=null){
            throw new BussinessException("邮箱已经注册");
        }
        String s = (String)redisTemplate.opsForValue().get(String.format(RegisterCon.REGISTER_KEY,dto.getEmail()));
        // 判断验证码是否正确
        if(!dto.getSmsCode().equals(s)){
            throw new BussinessException("验证码错误，请重试");
        }

        // 判断两次密码是否一致
        if(!dto.getPassword().equals(dto.getPasswordRepeat())){
            throw new BussinessException("两次密码不一致，请重试");
        }

        // 保存数据到数据库
        User user = dto.getUser();
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setDisable(false);
        logininfo.setType(1);
        logininfoMapper.add(logininfo);

        // 传入user中logininfoId
        user.setLogininfoId(logininfo.getId());
        userMapper.add(user);
    }
}
